﻿/*CLR Version: 4.0.30319.18408
 * Creat Date: 2015/6/18 16:08:36
 * Creat Year: 2015
 * Creator: 程炜.CW
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility.Extensions
{
    public static class NullableExtensions
    {
        /// <summary>
        /// 如果值为空,返回defaultVal
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="defaultVal">返回默认值</param>
        /// <returns></returns>
        public static DateTime Parse(this DateTime? dt, DateTime defaultVal)
        {
            if (null != dt)
            {
                return Convert.ToDateTime(dt);
            }
            return defaultVal;
        }
        /// <summary>
        /// 如果值为空,返回 defaultVal
        /// </summary>
        /// <param name="val"></param>
        /// <param name="defaultVal">默认值 0</param>
        /// <returns></returns>
        public static int Parse(this int? val, int defaultVal = 0)
        {
            if (null != val)
            {
                return Convert.ToInt32(val);
            }
            return defaultVal;
        }
        /// <summary>
        /// 如果值为空,返回0
        /// </summary>
        /// <param name="val"></param>
        /// <param name="defaultVal">默认值 0d</param>
        /// <returns></returns>
        public static double Parse(this double? val, double defaultVal = 0d)
        {
            if (null != val)
            {
                return Convert.ToDouble(val);
            }
            return defaultVal;
        }
        /// <summary>
        /// 如果值为空,返回false
        /// </summary>
        /// <param name="bl"></param>
        /// <param name="defaultVal">默认值 false</param>
        /// <returns></returns>
        public static Boolean Parse(this bool? bl, bool defaultVal = false)
        {
            if (null != bl)
            {
                return Convert.ToBoolean(bl);
            }
            return defaultVal;
        }
    }
}
